package com.android.scanner.widget;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.scanner.R;

public class EditDialog extends Dialog {

	private List<EditText> editTxtList = new ArrayList<EditText>();
	private List<Boolean> editTxtEnable = new ArrayList<Boolean>();

	private TextView txt1, txt2, txt3, txt4;
	private EditText txt1_value, txt2_value, txt3_value, txt4_value;

	private Button btnOk, btnCancel;

	private List<View> views = new ArrayList<View>();

	private int curPos = -1;

	private int style;
	public final static int BB_LIST_EDIT = 1;

	public EditDialog(Context context, Handler handler, int style, int theme) {
		super(context, R.style.myDialogTheme);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_dialog);
		txt1 = (TextView) findViewById(R.id.txt1);
		txt2 = (TextView) findViewById(R.id.txt2);
		txt3 = (TextView) findViewById(R.id.txt3);
		txt4 = (TextView) findViewById(R.id.txt4);

		txt1_value = (EditText) findViewById(R.id.txt1_value);
		txt2_value = (EditText) findViewById(R.id.txt2_value);
		txt3_value = (EditText) findViewById(R.id.txt3_value);
		txt4_value = (EditText) findViewById(R.id.txt4_value);

		btnOk = (Button) findViewById(R.id.btn_ok);
		btnCancel = (Button) findViewById(R.id.btn_cancel);

		views.add(txt1_value);
		views.add(txt2_value);
		views.add(txt3_value);
		views.add(txt4_value);
		views.add(btnOk);
		views.add(btnCancel);

		switch (style) {
		case BB_LIST_EDIT:
			txt1_value.setEnabled(false);
			txt2_value.setEnabled(false);
			txt3_value.setEnabled(false);
			txt4_value.setEnabled(false);
			break;
		}
		btnCancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				dismiss();
			}
		});

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch (keyCode) {
		case 20:
			if (curPos == -1)
				curPos = 0;
			else
				curPos = (curPos + 1) % views.size();
			views.get(curPos).requestFocus();
			break;
		case 19:
			if (curPos == -1 || curPos == 0)
				curPos = views.size() - 1;
			else
				curPos = curPos - 1;
			views.get(curPos).requestFocus();
			break;

		}
		return super.onKeyDown(keyCode, event);
	}

	public String getTxt1Value() {
		return txt1_value.getText().toString().trim();
	}

	public String getTxt2Value() {
		return txt1_value.getText().toString().trim();
	}

	public String getTxt3Value() {
		return txt1_value.getText().toString().trim();
	}

	public String getTxt4Value() {
		return txt1_value.getText().toString().trim();
	}
}
